﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Engine.Sgml.Configuration;
using Engine.Sgml.Merging;
using Engine.Sgml.Allocation;
using Engine.Dtd.Schema;

namespace Engine.Sgml.Validation.Rules {
    internal class SurroundRule : DefaultRule {
        public SurroundRule(SName name, ValidatorBehavior settings)
            : base(name, settings) {
            }

        public override bool CanSurround(InconsistentBlock block, SElement ancestor) {
            if (block.Count == 0)
                return false;
            IModelGroupsNode item = AllocationHelper.FindAncestor(block.FirstElement, ancestor);
            if (item == null)
                return false;
            for (int i = 1; i < block.Count; i++) {
                if (!item.Declaration.ContentModel.Exists(block[i].DName))
                    return false;
                }
            return true;
            }

        public override bool Surround(InconsistentBlock block, SElement ancestor) {
            IModelGroupsNode item = AllocationHelper.FindAncestor(block.FirstElement, ancestor);
            SElement wrapper = AllocationHelper.CreateWrapper(ancestor, item.Declaration);
            AllocationHelper.Surround(block, wrapper);
            block.Clear();
            return true;
            }
        }
    }
